import scrapy

from scrapys.wudiSpyder.wudiSpyder.items import MatchInfoItem

class Wan500LiveJingcaiSpider(scrapy.Spider):
    name = "Wan500LiveJingcaiSpider"
    allowed_domains = ["500.com"]
    start_urls = [
        "https://live.500.com",  # 足球竞猜彩页面
    ]

    def parse(self, response):
        game_expect = response.xpath('//*[@id="sel_expect"]/option/text()').get("data") # //*[@id="sel_expect"]/option[4]
        match_body = response.xpath('//*[@id="table_match"]/tbody')[0]
        match_body_trs = match_body.xpath("tr")
        # 先做一个数据清洗，防止有加时数据数据行
        match_rows = []

        for i in range(len(match_body_trs)):
            if not match_body_trs[i].xpath("@parentid").extract():
                match_rows.append(match_body_trs[i])
        for tr in match_rows:
            fid = tr.xpath("@fid").get("data")  # 500wan比赛id
            status = tr.xpath("@status").get("data")    # 500wan比赛状态，0：未开始，4：已结束
            game_no_num = tr.xpath("@order").get("data")  # 场次编号
            tds = tr.xpath("td")
            game_no = tds[0].xpath("text()").get("data")  # 场次名称
            contest = tds[1].xpath("a/text()").get("data")      # 赛事
            match_round = tds[2].xpath("text()").get("data")     # 轮次
            match_time = tds[3].xpath("text()").get("data")     # 比赛时间

            home_team = tds[5].xpath("a/text()").get("data")      # 主队名称
            home_team_url = tds[5].xpath("a/@href").get("data")  # 主队链接
            home_team_ranking = '[0]'
            home_ranking = tds[5].xpath("span/text()").extract()  # 主队排名//*[@id="a1150848"]/td[6]/span
            if len(home_ranking) > 0:
                home_team_ranking = home_ranking
            home_team_score = 0
            home_score = tds[6].xpath("div/a[1]/text()").extract()  # 主队得分
            if len(home_score) > 0:
                home_team_score = home_score

            match_line = tds[6].xpath("div/a[2]/text()").get("data")  # 澳门水位
            away_team_score = 0
            away_score = tds[6].xpath("div/a[3]/text()").extract()  # 客队得分
            if len(away_score) > 0:
                away_team_score = away_score[0]

            away_team = tds[7].xpath("a/text()").get("data")  # 客队名称
            away_team_ranking = '[0]'
            away_ranking = tds[7].xpath("span[@class='gray']/text()").extract()  # 客队排名//*[@id="a1181171"]/td[8]/span[2]
            if len(away_ranking) > 0:
                away_team_ranking = away_ranking
            away_team_url = tds[7].xpath("a/@href").get("data")  # 客队链接

            half_score = tds[8].xpath("text()").extract()   # 半场比分
            to_half_score = half_score[0].split("-")
            home_team_half_score = to_half_score[0].strip()
            away_team_half_score = to_half_score[1].strip()
            if not home_team_half_score:
                home_team_half_score = 0
                away_team_half_score = 0

            # 提取基本信息
            match_item = MatchInfoItem()
            match_item['fid'] = fid
            match_item['match_status'] = status
            match_item['contest'] = contest
            match_item['match_time'] = match_time
            match_item['match_round'] = match_round
            match_item['home_team'] = home_team
            match_item['home_team_ranking'] = home_team_ranking[0]
            match_item['home_team_url'] = home_team_url
            match_item['match_line'] = match_line
            match_item['away_team'] = away_team
            match_item['away_team_ranking'] = away_team_ranking[0]
            match_item['away_team_url'] = away_team_url

            match_item['home_team_half_score'] = home_team_half_score
            match_item['away_team_half_score'] = away_team_half_score
            match_item['home_team_score'] = home_team_score
            match_item['away_team_score'] = away_team_score

            match_item['odds'] = "{},{},{}".format(0, 0, 0)

            yield match_item


